/**********************************************************************************
// Explosion 
// 
// Creation:	12 Out 2009
// Updated:		20 Apr 2011
// Compiler:	Visual Studio 2010
//
// Notes:		Create explosions for asteroids game
//
**********************************************************************************/

#include "Explosion.h"

/*********************************************************************************/

Explosion::Explosion()
{
	// get counter frequency
	QueryPerformanceFrequency(&freq);

	// new sement for random number generator
	srand(GetTickCount());
}

/*********************************************************************************/

Explosion::~Explosion()
{
	for (ipar = particles.begin(); ipar != particles.end(); ++ipar)
		delete *ipar;

	particles.clear();
}

/*********************************************************************************/

bool Explosion::Active()
{
	// get elapsed time
	QueryPerformanceCounter(&end);
	LONGLONG remaining = duration - (end.QuadPart - start.QuadPart);
	LONGLONG secs = remaining / freq.QuadPart;
	LONGLONG milisecs = (remaining % freq.QuadPart) * 1000 / freq.QuadPart;

	// return true if duration is greather than zero
	if (remaining > 0)
		return true;
	else
		return false;
}

/*********************************************************************************/

void Explosion::InitParticles(float x, float y, int num, float lifetime, Color col)
{
	// set duration of animation
	duration = LONGLONG(lifetime * freq.QuadPart);

	// set the amount of color dimming: the lifetime in seconds divided by 1/30 of a second 
	// give us the number of times the colorDimming will be applied in a 30FPS game
	// divide the max color value (here we are considering 200) by that and 
	// we get the number that should be subtract from the color each frame
	colorDimming = 200 / int(lifetime / 0.033f);

	// start counter
	QueryPerformanceCounter(&start);

	Vector2 direction; 
	float ang, speed;
	Missile * mis;

	// create all particles
	for (int i = 0; i < num; ++i)
	{
		// set direction and speed of particles
		ang = float(rand()%360);
		speed = float(1 + (rand()%8));
		direction.SetVector(ang, speed);

		// create particle
		mis = new Missile(x, y, direction);
		mis->color = col;
		particles.push_back(mis);
	}
}

/*********************************************************************************/

void Explosion::DrawParticles()
{
	for (ipar = particles.begin(); ipar != particles.end(); ++ipar)
		(*ipar)->Draw();
}

/*********************************************************************************/

void Explosion::MoveParticles()
{
	for (ipar = particles.begin(); ipar != particles.end(); ++ipar)
	{
		// dim color for 1/3 of the particles
		if (!(rand()%3))
		{
			Color col = (*ipar)->color;
			(*ipar)->color = col - colorDimming;
		}

		// move the particles
		(*ipar)->Move();
	}
}

/*********************************************************************************/